﻿using System;

namespace TSites.Utility
{
	/// <summary>
	/// This is the base class for loggin purposes. 
	/// Any class that require to log errors/debug/info, it must be derived from this.
	/// </summary>
	public abstract class LoggerBase
	{
		#region Member Variables

		/// <summary>
		/// Member variable to hold the <see cref="ILog"/> instance.
		/// </summary>
		private readonly log4net.ILog logger = null;
		private bool _isInfoEnabled, _isWarnEnabled, _isErrorEnabled,
			_isDebugEnabled, _isFatalEnabled;

		#endregion

		#region Properties

		/// <summary>
		/// Abstract property which must be overridden by the derived classes.
		/// The logger prefix is used to create the logger instance.
		/// </summary>
		protected abstract Type LogPrefix
		{
			get;
		}

		#endregion

		#region Constructors

		/// <summary>
		/// Constructor of the class.
		/// </summary>
		public LoggerBase()
		{
			logger = log4net.LogManager.GetLogger(this.LogPrefix);
			_isInfoEnabled = logger.IsInfoEnabled;
			_isWarnEnabled = logger.IsWarnEnabled;
			_isErrorEnabled = logger.IsErrorEnabled;
			_isDebugEnabled = logger.IsDebugEnabled;
			_isFatalEnabled = logger.IsFatalEnabled;
		}

		#endregion

		#region Methods

		#region Protected Methods

		/// <summary>
		/// Information level messages are logged to the logger.
		/// </summary>
		/// <param name="message">String that needs to be logged.</param>
		public void Info(string message)
		{
			if (_isInfoEnabled)
			{
				this.logger.Info(message);
			}
		}

		/// <summary>
		/// Information level messages are logged to the logger.
		/// </summary>
		/// <param name="message">String that needs to be logged.</param>
		/// <param name="e">The exception that needs to be logged.</param>
		public void Info(string message, Exception e)
		{
			if (_isInfoEnabled)
			{
				this.logger.Info(message, e);
			}
		}

		/// <summary>
		/// Warning level messages are logged to the logger.
		/// </summary>
		/// <param name="message">String that needs to be logged.</param>
		public void Warn(string message)
		{
			if (_isWarnEnabled)
			{
				this.logger.Warn(message);
			}
		}

		/// <summary>
		/// Warning level messages are logged to the logger.
		/// </summary>
		/// <param name="message">String that needs to be logged.</param>
		/// <param name="e">The exception that needs to be logged.</param>
		public void Warn(string message, Exception e)
		{
			if (_isWarnEnabled)
			{
				this.logger.Warn(message, e);
			}
		}

		/// <summary>
		/// Error level messages are logged to the logger.
		/// </summary>
		/// <param name="message">String that needs to be logged.</param>
		public void Error(string message)
		{
			if (_isErrorEnabled)
			{
				this.logger.Error(message);
			}
		}

		/// <summary>
		/// Error level messages are logged to the logger.
		/// </summary>
		/// <param name="message">String that needs to be logged.</param>
		/// <param name="e">The exception that needs to be logged.</param>
		public void Error(string message, Exception e)
		{
			if (_isErrorEnabled)
			{
				this.logger.Error(message, e);
			}
		}

		/// <summary>
		/// Debug level messages are logged to the logger.
		/// </summary>
		/// <param name="message">String that needs to be logged</param>
		public void Debug(string message)
		{
			if (_isDebugEnabled)
			{
				this.logger.Debug(message);
			}
		}

		/// <summary>
		/// Debug level messages are logged to the logger.
		/// </summary>
		/// <param name="message">String that needs to be logged</param>
		/// <param name="e">The exception that needs to be logged</param>
		public void Debug(string message, Exception e)
		{
			if (_isDebugEnabled)
			{
				this.logger.Debug(message, e);
			}
		}

		/// <summary>
		/// Fatal level messages are logged to the logger.
		/// </summary>
		/// <param name="message">String that needs to be logged</param>
		public void Fatal(string message)
		{
			if (_isFatalEnabled)
			{
				this.logger.Fatal(message);
			}
		}

		/// <summary>
		/// Fatal level messages are logged to the logger.
		/// </summary>
		/// <param name="message">String that needs to be logged</param>
		/// <param name="e">The exception that needs to be logged</param>
		public void Fatal(string message, Exception e)
		{
			if (_isFatalEnabled)
			{
				this.logger.Fatal(message, e);
			}
		}

		#endregion

		#endregion
	}
}
